<template>
  <el-form label-width="100">
    <el-form-item label="SKU名称">
      <el-input v-model="skuParams.skuName" placeholder="SKU名称"></el-input>
    </el-form-item>
    <el-form-item label="价格(元)">
      <el-input v-model="skuParams.price" type="number" placeholder="价格(元)"></el-input>
    </el-form-item>
    <el-form-item label="重量(克)">
      <el-input v-model="skuParams.weight" type="number" placeholder="重量(克)"></el-input>
    </el-form-item>
    <el-form-item label="SKU描述">
      <el-input v-model="skuParams.skuDesc" type="textarea" placeholder="SKU描述"></el-input>
    </el-form-item>
    <!-- 平台属性 -->
    <el-form-item label="平台属性">
      <el-form inline class="skuform" label-width="100">
        <el-form-item v-for="item in attrArr" :key="item.id" :label="item.attrName" style="margin: 10px;">
          <el-select  v-model="item.attrIdAndValueId" placeholder="请选择平台属性">
            <el-option :value="`${item.id}:${attrValue.id}`" v-for="attrValue in item.attrValueList" :key="attrValue.id" :label="attrValue.valueName" ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <!-- 销售属性 -->
    <el-form-item label="销售属性">
      <el-form inline class="skuform" label-width="100">
        <el-form-item v-for="item in saleArr" :key="item.id" :label="item.saleAttrName" style="margin: 10px;">
          <el-select v-model="item.saleIdAndValueId" placeholder="请选择销售属性">
            <el-option :value="`${item.id}:${saleAttrValue.id}`" v-for="saleAttrValue in item.spuSaleAttrValueList" :key="saleAttrValue.id" :label="saleAttrValue.saleAttrValueName" ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <!-- 表格 -->
    <el-form-item label="图片名称">
      <el-table border :data="imgArr" ref="table" @select="selectImg">
        <el-table-column type="selection" width="80" align="center"></el-table-column>
        <el-table-column label="图片">
          <template #default="{row}">
            <img v-show="row.imgUrl.includes('blob')" src="@/assets/images/no-img.png" style="width: 120px;height: 120px;"/> 
            <img v-show="!row.imgUrl.includes('blob')" :src="row.imgUrl" style="width: 120px;height: 120px;"/> 
          </template>
        </el-table-column>
        <el-table-column label="名称" prop="imgName"></el-table-column>
        <el-table-column label="操作">
          <template #default="{row}">
            <el-button type="primary" @click="handlerImg(row)">设置默认</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form-item>
    <!-- 按钮 -->
    <el-form-item>
      <el-button type="primary" @click="save">保存</el-button>
      <el-button type="default" @click="cancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import type { SpuData } from '@/api/product/spu/type';
import { reqAttr } from '@/api/product/attr'
import {  reqAddSku, reqSaleAttrList, reqSpuImageList } from '@/api/product/spu'
import { reactive, ref } from 'vue'
import type { AttrList } from '@/api/product/attr/type';
import type { SaleAttrList, SpuImageList } from '@/api/product/attr/spu';
import type { SpuImage,SkuData } from '@/api/product/spu/type';
import { ElMessage } from 'element-plus';
// 取消事件
const cancel = () => {
  $emit('changeScene', {flag: 0})
}

// 定义收集sku参数
const skuParams = reactive<SkuData>({
  category3Id: '',
  spuId: '',
  tmId: '', 
  skuName: '',
  price: '',
  weight: '',
  skuDesc: '',
  skuDefaultImg:'',
  skuAttrValueList: [],//sku平台属性数组
  skuSaleAttrValueList: [] // sku销售属性数组
})

//初始化获取数据
// 平台属性
const attrArr = ref<AttrList>([])
// 销售属性
const saleArr = ref<SaleAttrList>([])
// spu图片
const imgArr = ref<SpuImageList>([])
// 清空数据
const clearData = () => {
  attrArr.value = []
  saleArr.value = []
  imgArr.value = []
  Object.assign(skuParams, {
    category3Id: '',
    spuId: '',
    tmId: '', 
    skuName: '',
    price: '',
    weight: '',
    skuDesc: '',
    skuDefaultImg:'',
    skuAttrValueList: [],//sku平台属性数组
    skuSaleAttrValueList: [] // sku销售属性数组
  })
}

const initSkuData = async (c1Id:number|string,c2Id:number|string,row:SpuData) => {
  // 清空数据
  clearData()
  skuParams.category3Id = row.category3Id
  skuParams.spuId = row.id as (number|string)
  skuParams.tmId = row.tmId
  // 获取平台属性
  const res1 = await reqAttr(c1Id,c2Id,row.category3Id)
  attrArr.value = res1.data
  // 获取销售属性
  const res2 = await reqSaleAttrList(row.id as number)
  saleArr.value = res2.data
  // spu图片列表
  const res3 = await reqSpuImageList(row.id as number)
  imgArr.value = res3.data
}


// 设置默认图片按钮 同时table前面复选框要勾上
const chooseImg = (row:SpuImage) => {
  imgArr.value.forEach((item:SpuImage) => {
    table.value.toggleRowSelection(item, false)
  })
  // 复选框选中
  table.value.toggleRowSelection(row, true)
  skuParams.skuDefaultImg = row.imgUrl as string
}
const table = ref()
const handlerImg = (row:SpuImage) => {
  // // 先全部取消勾选
  // imgArr.value.forEach((item:SpuImage) => {
  //   table.value.toggleRowSelection(item, false)
  // })

  // // 复选框选中
  // table.value.toggleRowSelection(row, true)
  // skuParams.skuDefaultImg = row.imgUrl as string
  chooseImg(row)
}

// 复选框选中
const selectImg = (rowArr:SpuImage[], row:SpuImage) => {
  chooseImg(row)
}



// 保存
const save = async () => {
  // 整理参数
  // 1.平台属性
  skuParams.skuAttrValueList = attrArr.value.reduce((prev:any,next:any) => {
    // prev第一次是[]
    if(next.attrIdAndValueId) {
      const [attrId, valueId] = next.attrIdAndValueId.split(':')
      prev.push({
        attrId, 
        valueId
      })
    }
    return prev
  }, [])
  // 2.销售属性
  skuParams.skuSaleAttrValueList = saleArr.value.reduce((prev:any,next:any) => {
    if(next.saleIdAndValueId) {
      const [ saleAttrId, saleAttrValueId] = next.saleIdAndValueId.split(':')
      prev.push({
        saleAttrId, 
        saleAttrValueId
      })
    }
    return prev
  }, [])

  const res = await reqAddSku(skuParams)
  if(res.code == 200) {
    ElMessage({
      type: 'success',
      message:'添加成功'
    })
    $emit('changeScene', {flag:0,})
  } else {
    ElMessage({
      type: 'error',
      message:'添加失败'
    })
  }

}

// const $emit = defineEmits<{
//   (e:'changeScene', data:{flag:number,params?:string}):void
// }>()

const $emit = defineEmits<{
  changeScene: [{flag:number, params?:string}]
}>()
defineExpose({
  initSkuData
})

</script>

<style lang="scss">
  .skuform {
    .el-input, .el-select {
      width: 200px;
    }
  }
</style>
